package com.link2room.ironhide.service.impl;

import com.link2room.common.constant.DefaultValue;
import com.link2room.common.jpa.result.QueryResult;
import com.link2room.common.service.ICommonService;
import com.link2room.common.util.ClassConverUtil;
import com.link2room.common.util.StringUtil;
import com.link2room.enums.exception.IronhideSysExceptionEnum;
import com.link2room.ironhide.entity.L2RHotelPosition;
import com.link2room.ironhide.exception.IronhideException;
import com.link2room.ironhide.dto.result.base.PositionBaseDto;
import com.link2room.ironhide.service.IL2RPositionService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by sss on 2016/10/12.
 */
@Service("l2RPositionService")
public class L2RPositionServiceImpl implements IL2RPositionService {
    @Resource
    private ICommonService commonService;

    /**
     *
     * @param <S> 源CLASS
     * @param lhotelGroupCode
     * @param lhotelCode
     * @param queryObj
     * @param sourceClass
     * @return
     * @throws Exception
     */
    @Override
    public <S>List<PositionBaseDto> listPositionBase(
            String lhotelGroupCode, String lhotelCode, S queryObj,
            Class<S> sourceClass)
            throws Exception{

        QueryResult<S> positionBaseResult = commonService.findAllEQ(sourceClass, queryObj);
        QueryResult<PositionBaseDto> positionBaseDtoResult= ClassConverUtil.copyQueryResultToDtoQueryResult(positionBaseResult,PositionBaseDto.class, false);
        List<PositionBaseDto> positionBaseList = positionBaseDtoResult.getResultlist();
        List<PositionBaseDto> topList=new ArrayList<>();
        List<PositionBaseDto> tempList=new ArrayList<>();

        for(PositionBaseDto positionBase:positionBaseList){
            positionBase.setLhotelGroupCode(lhotelGroupCode);
            positionBase.setListOrder(null);
            positionBase.setLhotelCode(null);
            positionBase.setId(null);
            if("ROOT".equals(positionBase.getPositionType())){
                //取出顶级节点
                positionBase.setPositionKey(this.getPositionKey(positionBase.getPositionType(),positionBase.getPositionCode()));
                topList.add(positionBase);
            }else {
                //保存子节点
                tempList.add(positionBase);
            }
        }
        for(PositionBaseDto top:topList){
            List<PositionBaseDto> childList=childNodeList(tempList,top.getPositionCode());
            top.setChildren(childList);
        }
        return topList;
    }

    @Override
    @Transactional
    public void saveOrUpdateHotelPositions(String lhotelGroupCode,String lhotelCode,List<L2RHotelPosition> l2RHotelPositionList)throws Exception{
        if(StringUtil.isNotBlank(lhotelCode)){
            L2RHotelPosition queryL2RHP=new L2RHotelPosition();
            queryL2RHP.setLhotelGroupCode(lhotelGroupCode);
            queryL2RHP.setLhotelCode(lhotelCode);
            commonService.deleteAllEQ(L2RHotelPosition.class,queryL2RHP);
            commonService.saveOrUpdateList(l2RHotelPositionList);
        }else {
            throw new IronhideException(IronhideSysExceptionEnum.HOTELS_NOT_EXIT);
        }

    }

    private List<PositionBaseDto> childNodeList(List<PositionBaseDto> inList, String parentCode)throws Exception{
        List<PositionBaseDto> mainList=new ArrayList<>();
        List<PositionBaseDto> tempList=new ArrayList<>();

        for(PositionBaseDto in:inList){
            if(parentCode.equals(in.getPositionType())){
                in.setPositionKey(this.getPositionKey(in.getPositionType(),in.getPositionCode()));
                if("District".equals(in.getPositionType())){
                    if(!"市辖区".equals(in.getName())){
                        mainList.add(in);
                    }
                }else {
                    mainList.add(in);
                }
            }else {
                tempList.add(in);
            }
        }

        for(PositionBaseDto main:mainList){
            List<PositionBaseDto> childList=childNodeList(tempList,main.getPositionCode());
            main.setChildren(childList);
        }
        return mainList;
    }

    private String getPositionKey(String parentCode,String childCode){
        return parentCode+ DefaultValue.CUT+childCode;
    }

}
